package com.td.filemanager.business.filetype;

import com.td.filemanager.ResourceTable;
import com.td.filemanager.business.device.DeviceManagerViewModel;
import com.td.filemanager.business.filethumbnail.FileThumbnailAbilitySlice;
import com.td.filemanager.provider.ListComponentProvider;
import com.td.filemanager.slice.BaseAbilitySlice;
import com.td.filemanager.ui.widget.ActionBar;
import com.td.lib.viewmodel.DefaultDataObserver;
import com.td.lib.viewmodel.ViewModelAbility;
import com.td.lib.viewmodel.ViewModelProvider;
import com.td.library.data.FileTypeData;
import com.td.library.utils.LogUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

import java.util.List;


/**
 * 文件管理页面
 */
public class FileManagerAbilitySlice extends BaseAbilitySlice {
    private static final String TAG = FileManagerAbilitySlice.class.getSimpleName();

    private ListContainer mListContainer;
    private Component mConfirm;
    private Component mRefresh;
    private Checkbox mSelectAllComponent;
    private Component mSeleckAllContainer;

    private FileTypeDataProvider mFileDataProvider;

    private FileTypeViewModel mViewModel;
    private ActionBar mActionBar;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_slice_file_type);
        initComponent();
        initViewModel();
        initListener();
        initObserver();
    }


    private void initComponent() {
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        mConfirm = findComponentById(ResourceTable.Id_confirm);
        mRefresh = findComponentById(ResourceTable.Id_refresh);
        mSelectAllComponent = (Checkbox) findComponentById(ResourceTable.Id_seleckAll);
        mSeleckAllContainer = findComponentById(ResourceTable.Id_seleckAllContainer);
        mActionBar = (ActionBar) findComponentById(ResourceTable.Id_actionBar);
        mActionBar.setLeftClickListener(component -> {
            terminate();
        });
    }

    private void initListener() {
        mSeleckAllContainer.setClickedListener(component -> {
            LogUtil.info(TAG, "mSeleckAllContainer setClickedListener");
            mSelectAllComponent.setChecked(!mSelectAllComponent.isChecked());
        });
        mSelectAllComponent.setCheckedStateChangedListener((absButton, data) -> {
            LogUtil.info(TAG, "onCheckedChanged " + data);
            mViewModel.getSelectAll().setData(data);
        });
        mConfirm.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new FileThumbnailAbilitySlice(),
                        new Intent());
            }
        });
        mRefresh.setClickedListener(component -> {
        });
    }


    private void initViewModel() {
        if (getAbility() instanceof ViewModelAbility) {
            ViewModelAbility viewModelAbility = (ViewModelAbility) getAbility();
            mViewModel = new ViewModelProvider(viewModelAbility.getViewModelStore())
                    .get(FileTypeViewModel.class);
            DeviceManagerViewModel deviceManagerViewModel =
                    new ViewModelProvider(viewModelAbility.getViewModelStore()).get(DeviceManagerViewModel.class);
            mViewModel.setSelectDeviceData(deviceManagerViewModel.getSelectedDevices());
            mViewModel.setParentFilePath(viewModelAbility.getDistributedDir().getPath());
            mViewModel.refresh();
        }
    }

    private void initObserver() {
        mViewModel.getFileTypeData().addObserver(
                new DefaultDataObserver<List<FileTypeData>>(getLifecycle()) {
                    @Override
                    public void onChanged(List<FileTypeData> fileTypeData) {
                        LogUtil.info(TAG, "onChanged getDevicedata " + fileTypeData);
                        if (mFileDataProvider == null) {
                            initDeviceDataProvider(fileTypeData);
                        } else {
                            mFileDataProvider.notifyDataChanged();
                        }
                        boolean allSelect = fileTypeData.stream().allMatch(FileTypeData::isChecked);
                        if (!allSelect) {
                            mSelectAllComponent.setChecked(false);
                        }
                    }
                }, false);

        mViewModel.getSelectAll().addObserver(new DefaultDataObserver<Boolean>(getLifecycle()) {
            @Override
            public void onChanged(Boolean aBoolean) {
                LogUtil.info(TAG, "FileManagerAbilitySlice onChanged getSelectAll " + aBoolean);
                mSelectAllComponent.setChecked(aBoolean);
                List<FileTypeData> deviceData = mViewModel.getFileTypeData().getData();
                if (deviceData != null) {
                    deviceData.forEach(device -> {
                        device.setChecked(aBoolean);
                    });
                    mViewModel.getFileTypeData().setData(deviceData);
                }
            }
        }, false);
    }

    private void initDeviceDataProvider(List<FileTypeData> deviceData) {
        mFileDataProvider = new FileTypeDataProvider(FileManagerAbilitySlice.this,
                deviceData, ResourceTable.Layout_file_type_item);
        mListContainer.setItemProvider(mFileDataProvider);

        mFileDataProvider.setOnItemClickListener(new ListComponentProvider.OnItemClickListener<FileTypeData>() {
            @Override
            public void onItemClick(Component component, FileTypeData item, int position) {
                LogUtil.info(TAG, "onItemClick " + position + " item " + item);
                item.setChecked(!item.isChecked());
                mFileDataProvider.notifyDataSetItemChanged(position);
            }
        });
    }
}
